#include "draw.h"

draw::draw(std::vector<std::vector<int>> vertex, pair_int start, pair_int end) {
    
    N = vertex[0].size();

    map = cv::Mat(N * 10, N * 10, CV_8UC3, cv::Scalar(255, 255, 255));

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (vertex[i][j]) {
                cv::rectangle(map, cv::Point(j * 10, i * 10), cv::Point((j + 1) * 10, (i + 1) * 10), cv::Scalar(160, 160, 160), -1);
            }
        }
    }

    //画线
    for (int i = 1; i < N; i++)
        cv::line(map, cv::Point(0, (i * 10)), cv::Point(200, (i * 10)), cv::Scalar(0, 0, 0));
    for (int i = 1; i < N; i++)
        cv::line(map, cv::Point((i * 10), 0), cv::Point((i * 10), 200), cv::Scalar(0, 0, 0));

    //this->end = end;
    position = start;
}

void draw::move(Direction direction) {
    switch (direction) {
        case RIGHT:
            for (int i = 0; i < 10; i++) {
                cv::circle(map, cv::Point(position.first++, position.second), 0, cv::Scalar(10, 10, 200), -1);
                cv::imshow("map", map);
                cv::waitKey(100);
            }
            break;
        case LOWERRIGHT:
            for (int i = 0; i < 10; i++) {
                cv::circle(map, cv::Point(position.first++, position.second++), 0, cv::Scalar(10, 10, 200), -1);
                cv::imshow("map", map);
                cv::waitKey(100);
            }
            break;
        case LOWER:
            for (int i = 0; i < 10; i++) {
                cv::circle(map, cv::Point(position.first, position.second++), 0, cv::Scalar(10, 10, 200), -1);
                cv::imshow("map", map);
                cv::waitKey(100);
            }
            break;
        case LOWERLEFT:
            for (int i = 0; i < 10; i++) {
                cv::circle(map, cv::Point(position.first--, position.second++), 0, cv::Scalar(10, 10, 200), -1);
                cv::imshow("map", map);
                cv::waitKey(100);
            }
            break;
        case LEFT:
            for (int i = 0; i < 10; i++) {
                cv::circle(map, cv::Point(position.first--, position.second), 0, cv::Scalar(10, 10, 200), -1);
                cv::imshow("map", map);
                cv::waitKey(100);
            }
            break;
        case UPPERLEFT:
            for (int i = 0; i < 10; i++) {
                cv::circle(map, cv::Point(position.first--, position.second--), 0, cv::Scalar(10, 10, 200), -1);
                cv::imshow("map", map);
                cv::waitKey(100);
            }
            break;
        case UPPER:
            for (int i = 0; i < 10; i++) {
                cv::circle(map, cv::Point(position.first, position.second--), 0, cv::Scalar(10, 10, 200), -1);
                cv::imshow("map", map);
                cv::waitKey(100);
            }
            break;
        case UPPERRIGHT:
            for (int i = 0; i < 10; i++) {
                cv::circle(map, cv::Point(position.first++, position.second--), 0, cv::Scalar(10, 10, 200), -1);
                cv::imshow("map", map);
                cv::waitKey(100);
            }
            break;
    }
}

Direction inline draw::judgeDirection(pair_int start, pair_int end) {
    if((end.first - start.first) == 1 && !(end.second - start.second)) {
        return RIGHT;
    } else if((end.first - start.first) == 1 && (end.second - start.second) == 1){
        return LOWERRIGHT;
    } else if(!(end.first - start.first) && (end.second - start.second) == 1){
        return LOWER;
    } else if((end.first - start.first) == -1 && (end.second - start.second) == 1){
        return LOWERLEFT;
    } else if((end.first - start.first) == -1 && !(end.second - start.second)){
        return LEFT;
    } else if((end.first - start.first) == -1 && (end.second - start.second) == -1){
        return UPPERLEFT;
    } else if(!(end.first - start.first) && (end.second - start.second) == -1){
        return UPPER;
    } else if((end.first - start.first) == 1 && (end.second - start.second) == -1){
        return UPPERRIGHT;
    }
}

void draw::coordinateToMap(pair_int &p){
    p.first = p.first * 10 + 5;
    p.second = p.second * 10 + 5;
    return;
}

void draw::show(pair_int start, pair_int end) {
    cv::namedWindow("map", cv::WINDOW_NORMAL);
    //end.first = 5;
    std::vector<pair_int> vp;
    position = start;

//    while (position != endl) {
//        move(LOWERRIGHT);
//    }
    Direction direct = judgeDirection(start, end);
    coordinateToMap(position);
    move(direct);

    cv::imshow("map", map);

    //cv::waitKey(0);

    return;
}

void draw::show(std::vector<pair_int> route) {
    cv::namedWindow("map", cv::WINDOW_NORMAL);
    Direction direct;
    int k = route.size();
    for(int i = 1; i < k; ++i){
        direct = judgeDirection(route[i-1], route[i]);
        position = route[i-1];
        coordinateToMap(position);
        move(direct);
    }

    //cv::imshow("map", map);


    return;
}